from django.db import models
from datetime import datetime

# Create your models here.


# 角色表
class RoleModel(models.Model):
    role_name = models.CharField(max_length=36, default='', blank=True, verbose_name='角色名称')

    def __str__(self):
        return self.role_name

    class Meta:
        db_table = 'role_model'


# 权限表
class CompetenceModel(models.Model):
    competence = models.CharField(max_length=36, default='', blank=True, verbose_name='权限名称')
    routing = models.CharField(max_length=64, default='', blank=True, verbose_name='路由')
    role_competence = models.ManyToManyField(to=RoleModel, verbose_name='权限多对多')

    def __str__(self):
        return self.competence

    class Meta:
        db_table = 'competence_model'


# 部门表
class DepartmentModel(models.Model):
    department_name = models.CharField(max_length=36, default='', blank=True, verbose_name='部门名称')

    def __str__(self):
        return self.department_name

    class Meta:
        db_table = 'department_model'


# 岗位表
class JobModel(models.Model):
    job_name = models.CharField(max_length=36, default='', blank=True, verbose_name='岗位信息')

    def __str__(self):
        return self.job_name

    class Meta:
        db_table = 'job_model'


# 职级表
class RankModel(models.Model):
    rank_name = models.CharField(max_length=36, default='', blank=True, verbose_name='职级名称')

    def __str__(self):
        return self.rank_name

    class Meta:
        db_table = 'rank_model'


# 用户表
class UserModel(models.Model):
    username = models.CharField(max_length=36, default='', blank=True, verbose_name='账号')
    name = models.CharField(max_length=36, default='', blank=True, verbose_name='名字')
    password = models.CharField(max_length=999, default='', blank=True, verbose_name='密码')
    role = models.ForeignKey(to=RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')
    birthday = models.DateField(verbose_name='出生日期', default='', blank=True)
    job = models.ForeignKey(to=JobModel, on_delete=models.CASCADE, verbose_name='所属岗位')
    rank = models.ForeignKey(to=RankModel, on_delete=models.CASCADE, verbose_name='所属职级')
    mobile = models.CharField(max_length=36, default='', blank=True, verbose_name='手机号')
    region = models.CharField(max_length=64, default='', blank=True, verbose_name='出生地区--城市')
    department = models.ForeignKey(to=DepartmentModel, on_delete=models.CASCADE, verbose_name='所属部门')
    mailbox = models.CharField(max_length=64, default='', blank=True, verbose_name='邮箱地址')
    portrait = models.CharField(max_length=999, default='', blank=True, verbose_name='头像')
    gender = models.CharField(max_length=36, default='', blank=True, verbose_name='性别')
    operating_time = models.DateTimeField(blank=True,default=datetime.now, verbose_name='创建日期')

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'user_model'


# 操作日志
class OperationLogModel(models.Model):
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='所属用户')
    operating_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')
    matter = models.CharField(max_length=64, default='', blank=True, verbose_name='操作事项')

    def __str__(self):
        return self.operating_time

    class Meta:
        db_table = 'operation_log_model'


# 登入日志
class LoginLogModel(models.Model):
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='所属用户')
    operating_time = models.DateTimeField(auto_now_add=True, verbose_name='登入时间')
    login_ip = models.CharField(max_length=64, default='', blank=True, verbose_name='登入ip')
    login_position = models.CharField(max_length=64, default='', blank=True, verbose_name='登入位置')
    login_equipment = models.CharField(max_length=64, default='', blank=True, verbose_name='登入设备')

    def __str__(self):
        return self.operating_time

    class Meta:
        db_table = 'login_log_model'


# 文件材料
class DocumentationModel(models.Model):
    documentation_name = models.CharField(max_length=64, default='', blank=True, verbose_name='文件名称')
    content = models.CharField(max_length=999, default='', blank=True, verbose_name='文件内容')
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='所属用户')
    address = models.CharField(max_length=999, default='', blank=True, verbose_name='文件地址')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return self.documentation_name

    class Meta:
        db_table = 'documentation_model'


# 邮箱表
class MailboxModel(models.Model):
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='发件人id')
    addressee = models.CharField(max_length=64, default='', blank=True, verbose_name='收件人邮箱账号')
    title = models.CharField(max_length=64, default='', blank=True, verbose_name='标题')
    status = models.IntegerField(verbose_name='状态 0待发送 1发送成功 2发送失败....')
    is_read = models.IntegerField(verbose_name='是否读 1已读 2未读....')
    annex = models.CharField(max_length=999, default='', blank=True, verbose_name='附件')
    content = models.CharField(max_length=1999, default='', blank=True, verbose_name='内容')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'mailbox_model'


# 抄送表
class ChaoSongModel(models.Model):
    mailbox = models.CharField(max_length=999, default='', blank=True, verbose_name='抄送人邮箱账号')
    mailbox_you = models.ForeignKey(to=MailboxModel, on_delete=models.CASCADE, verbose_name='邮箱id')

    def __str__(self):
        return self.mailbox

    class Meta:
        db_table = 'chao_song_model'


# 菜单表
class MenuModel(models.Model):
    menu_name = models.CharField(max_length=64, default='', blank=True, verbose_name='菜单名称')
    father = models.IntegerField(verbose_name='父级菜单 自关联关系')

    def __str__(self):
        return self.menu_name

    class Meta:
        db_table = 'menu_model'


# 车辆信息
class VehicleModel(models.Model):
    number = models.CharField(max_length=64, default='', blank=True, verbose_name='车牌号')
    version = models.CharField(max_length=64, default='', blank=True, verbose_name='型号')
    status = models.IntegerField(verbose_name='是否可用 1可用 2不可以')

    def __str__(self):
        return self.number

    class Meta:
        db_table = 'vehicle_model'


# 工资条
class SalarySlipModel(models.Model):
    name = models.CharField(max_length=64, default='', blank=True, verbose_name='姓名')
    wage = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='工资')
    ask_for_leave = models.IntegerField(verbose_name='请假天数')
    attendance = models.IntegerField(verbose_name='出勤天数')
    basic_wage = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='基本工资')
    job_wage = models.DecimalField(max_digits=18, decimal_places=2, verbose_name='岗位工资')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'salary_model'


# 流程表
class FlowModel(models.Model):
    flow_name = models.CharField(max_length=64, default='', blank=True, verbose_name='流程流程')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    is_take = models.IntegerField(verbose_name='是否启用 1是 2否')

    def __str__(self):
        return self.flow_name

    class Meta:
        db_table = 'flow_model'


# 流程节点表
class FlowNodeModel(models.Model):
    node_name = models.CharField(max_length=64, default='', blank=True, verbose_name='节点名称')
    flow = models.ForeignKey(to=FlowModel, on_delete=models.CASCADE, verbose_name='流程id')

    def __str__(self):
        return self.node_name

    class Meta:
        db_table = 'node_model'


# 节点属性表
class FlowPropertyModel(models.Model):
    type_name = models.CharField(max_length=20, verbose_name='属性名称')
    types = models.CharField(max_length=20, verbose_name='属性类型')
    node = models.ForeignKey(to=FlowNodeModel, on_delete=models.CASCADE, verbose_name='所属节点')

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = 'type_model'


# 节点属性实例表
class TypeInstanceModel(models.Model):
    type_data = models.CharField(max_length=20, verbose_name='属性内容')
    type = models.ForeignKey(to=FlowPropertyModel, on_delete=models.CASCADE, verbose_name=';所属节点属性')
    node = models.ForeignKey(to=FlowNodeModel, on_delete=models.CASCADE, verbose_name='所属节点')

    def __str__(self):
        return self.type_data

    class Meta:
        db_table = 'type_instance_model'


# 流程实例表
class ProcessInstanceModel(models.Model):
    employee = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name="所属员工")
    process = models.ForeignKey(to=FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')
    node = models.ForeignKey(to=FlowNodeModel, on_delete=models.CASCADE, verbose_name='所属节点')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return self.create_time

    class Meta:
        db_table = 'process_instance_model'


# 流程实例审批表
class ApporvalModel(models.Model):
    employee = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='所属员工id')
    approval_name = models.CharField(max_length=20, verbose_name='审批人姓名')
    approval_time = models.DateTimeField(auto_now_add=True, verbose_name='审批时间')
    approval_data = models.CharField(max_length=256, verbose_name='审批意见')
    approval_state = models.CharField(max_length=4, default=0, verbose_name='0未审批，1通过，2不通过')
    process_instance = models.ForeignKey(to=ProcessInstanceModel, on_delete=models.CASCADE, verbose_name='所属流程实例')

    def __str__(self):
        return self.approval_name

    class Meta:
        db_table = 'approval_model'
